*Recoding ZA4586 

use "Data\ZA4586\ZA4586_v1-0-0.dta" , clear

*privater pfad zum schnelleren Laden
*cd "C:\Users\alschrei\Desktop"
*use "ZA4586_v1-0-0.dta", clear

*Support for democracy
 *not included
 
*Support for liberal democracy
gen $libdem_opposition			= pd07
gen $libdem_freeopinion			= pd05
gen $libdem_partyopportunity	= pd08
gen $libdem_criticism			= pd06							
gen $libdem_conflict			= pd01							
gen $libdem_generalwill			= pd09
	
*Satisfaction with democracy
gen $satisfaction_democracy		= ps03

*Vertrauen Institutionen
gen $trust_parties = pt15
gen $trust_bundestag = pt03
gen $trust_government = pt12
gen $trust_verfassungsgericht = pt02
gen $trust_justice = pt08
gen $trust_administration = pt04

*Efficacy
gen $politicians_takecare = pe01
gen $politicians_interestpublic = pe05
gen $ego_politicdiscuss = pe02
gen $ego_politicscomplicated = pe04


*define missing values
mvdecode $libdem_opposition $libdem_freeopinion $libdem_partyopportunity ///
	$libdem_criticism $libdem_conflict $libdem_generalwill $satisfaction_democracy ///
	$politicians_takecare $politicians_interestpublic $ego_politicdiscuss $ego_politicscomplicated ///
	$trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice $trust_administration, ///
	mv(-11=.s\-9=.a\-8=.b\-1=.c)  //.a = no answer; .b = don't know; .c = not asked; .s = split



*Standardize range from 0 to 1; higher values indicate higher support for democracy
*reverse scale if necessary and standardize

foreach var of varlist $libdem_opposition $libdem_freeopinion $libdem_partyopportunity $satisfaction_democracy {
	replace `var'	= (6-`var')/5 if !missing(`var')
}

*standardize remaining items 

foreach var of varlist $libdem_criticism $libdem_conflict $libdem_generalwill {
	replace `var'	=(`var'-1)/5 if !missing(`var')
}

*standardize

foreach var of varlist $trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice $trust_administration {
	replace `var' = (`var' - 1)/6 if !missing(`var')
}

foreach var of varlist $politicians_takecare $ego_politicscomplicated {
	replace `var' = (`var' - 1)/3 if !missing(`var')
}

foreach var of varlist $politicians_interestpublic $ego_politicdiscuss {
	replace `var' = (4 - `var')/3 if !missing(`var')
}

*Sociodemographic Controls
gen			$gender 			= sex-1

*age: already defined, but missing values; also: drop value label and variable label
replace age						= .	if age == -32
label drop 						age											//remove value label
label var age ""																//remove variable label

gen 		$education			= educ
*define missing values 
mvdecode $education, mv(-9=.a\-41=.f)  //.a = no answer; .f= = data error 
replace 	$education 			= 0 if $education != 5 	& !missing($education)	// still at school, other, none, Hautpschule, Mittlere Reife, Fachhochschulreife = 0
replace 	$education 			= 1 if $education == 5							// Hochschulreife = 1

gen 		$eastwest 				= eastwest-1									 

*Weight
gen weight = wghtptew


*Party ID
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyid_category		= 1 	if inlist(pa04, 1, 3)
replace $partyid_category	= 0 	if inlist(pa04, 2, 4, 6)
replace $partyid_category	= -1 	if inlist(pa04, 5, 20, 30, 31, 40, 90)


*Party Vote
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyvote_category		= 1 	if inlist(pv01, 1, 3, 42)
replace $partyvote_category	= 0 	if inlist(pv01, 2, 4, 6)
replace $partyvote_category	= -1 	if inlist(pv01, 5, 20, 30, 41, 90)

*Birthplace
*Weas R born in eastern or western Germany?
* 1: east, 0: west 
gen $born_eastwest			= 1		if inlist(dg04, 1, 2)
replace $born_eastwest		= 0		if inlist(dg04, 3, 4)


*Meta variables
gen $dataset 					= "ZA4586"
gen $year 						= year


*check
tab $libdem_opposition pd07, m
tab $libdem_freeopinion pd05, m
tab $libdem_partyopportunity pd08, m
tab $libdem_criticism pd06, m						
tab $libdem_conflict pd01, m					
tab $libdem_generalwill pd09, m	
tab $satisfaction_democracy ps03, m
tab $gender sex, m
tab $age, m
tab $education educ, m
tab $eastwest eastwest, m	


*cd "M:\Hiwi\Alexander\Support for Democracy in Germany"

*Save recode dataset
keep $libdem_opposition $libdem_freeopinion $libdem_partyopportunity 									///
	$libdem_criticism	$libdem_conflict $libdem_generalwill $satisfaction_democracy $gender $age 		///
	$politicians_takecare $politicians_interestpublic $ego_politicdiscuss $ego_politicscomplicated ///
	$trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice $trust_administration ///
	$education $eastwest $dataset $year  weight	$partyid_category	$partyvote_category	
keep if $year == 1982 | $year == 1984 | $year == 1988 | $year == 1991 | $year == 1992 | $year == 1994 ///
 | $year == 1998 | $year == 2000 | $year == 2002 | $year == 2008 | $year == 2012 | $year == 2014
save ZA4586_recoded.dta, replace
